const express = require('express')
const db = require('./db/connect')
const path = require('path')
const app = express()
const bodyParser = require('body-parser')
const request = require('request')

//通过cors解决跨域
const cors = require('cors')
app.use(cors())

//解析请求体
app.use(bodyParser.urlencoded({ extended: false}))
app.use(bodyParser.json())

//静态目录
app.use('/public', express.static(path.join(__dirname,'./public')))

//路由
const userRouter = require('./router/userRouter')
app.use('/user', userRouter)

const foodRouter = require('./router/foodRouter')
app.use('/food', foodRouter)

const fileRouter = require('./router/fileRouter')
app.use('/file', fileRouter)

// //代理方式处理跨域->proxyTest.html (唯一可由前端完全处理的方式, jsonp/cors需要后端协同)
// app.get('/proxyTest', (req, res)=>{
//     request('http://www.baidu.com', (err, response, body)=>{
//         if(!err){
//             res.send(body)
//         }
//     })
// })


//Port
app.listen(3000, ()=>{
    console.log('Server start !')
})